﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
namespace DAO
{
    public class DatVanChuyenDAO : DataProvider
    {
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            DatVanChuyenDTO objectDTO = new DatVanChuyenDTO();
            objectDTO.MaDatVanChuyen = int.Parse(dt.Rows[i]["Gui"].ToString());
            objectDTO.MaMonHang = int.Parse(dt.Rows[i]["MaChiNhanh"].ToString());
            objectDTO.NgayLayHang = DateTime.Parse(dt.Rows[i]["NgayLayHang"].ToString());
            objectDTO.DiaChiLayHang = dt.Rows[i]["DiaChiLayHang"].ToString();
            objectDTO.MaLoaiVC = int.Parse(dt.Rows[i]["MaLoaiVC "].ToString());
            objectDTO.MaDiaDiem = int.Parse(dt.Rows[i]["MaDiaDiem"].ToString());
            objectDTO.NgayDat = DateTime.Parse(dt.Rows[i]["NgayDat"].ToString());
            return (object)objectDTO;
        }

        public DatVanChuyenDTO[] GetAll()
        {
            String strSQL = "Select * from DATVANCHUYEN,LOAIVANCHUYEN,CHINHANH,MONHANG Where DATVANCHUYEN.MaMonHang = MONHANG.MaMonHang and DATVANCHUYEN.MaLoaiVC=LOAIVANCHUYEN.MaLoaiVC and DATVANCHUYEN.MaDiaDiem = CHINHANH.MaChiNhanh ";
            DataTable dt = new DataProvider().ExecuteQuery(strSQL);

            DatVanChuyenDTO[] datvanchuyenDTOs = new DatVanChuyenDTO[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
                datvanchuyenDTOs[i] = (DatVanChuyenDTO)GetDataFromDataRow(dt, i);

            return datvanchuyenDTOs;
        }
        public DatVanChuyenDTO[] GetListByMaKhachHang(int MaKhachHang)
        {
            String strSQL = "Select DATVANCHUYEN.MaDatVanChuyen,DATVANCHUYEN.NgayDat,DATVANCHUYEN.MaMonHang,DATVANCHUYEN.NgayLayHang, DATVANCHUYEN.DiaChiLayHang,DATVANCHUYEN.MaLoaiVC,DATVANCHUYEN.MaDiaDiem from DATVANCHUYEN,MONHANG,KHACHHANG Where DATVANCHUYEN.MaMonHang = MONHANG.MaMonHang and MONHANG.MaKhachHangGui = KHACHHANG.MaKhachHang and KHACHHANG.MaKhachHang =" + MaKhachHang;
            DataTable dt = new DataProvider().ExecuteQuery(strSQL);

            DatVanChuyenDTO[] datvanchuyenDTOs = new DatVanChuyenDTO[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
                datvanchuyenDTOs[i] = (DatVanChuyenDTO)GetDataFromDataRow(dt, i);

            return datvanchuyenDTOs;
        }

        public bool insert(DatVanChuyenDTO info)
        {
            string insertCommand = "INSERT INTO DATVANCHUYEN(MaMonhang,NgayLayHang,DiaChiLayhang,MaLoaiVC,MaDiaDiem,NgayDat) VALUES(" +
                info.MaMonHang + ", '" +
                info.NgayLayHang.ToShortDateString() + "', '" +
                info.DiaChiLayHang + "', " +
                info.MaLoaiVC + ", " +
                info.MaDiaDiem + ",'" +
                info.NgayDat + "')";

            return ExecuteNonQuery(insertCommand);
        }

        public void update(DatVanChuyenDTO info)
        {
            string updateCommand = "UPDATE DATVANCHUYEN " +
            "SET MaMonHang = " + info.MaMonHang + ", " +
            " DiaChiLayHang = '" + info.DiaChiLayHang + "', " +
            " NgayLayHang = '" + info.NgayLayHang.ToShortDateString() + "', " +
            " MaLoaiVC = " + info.MaLoaiVC + "," +
            " MaDiaDiem = " + info.MaDiaDiem +
            " WHERE MaDatVanChuyen = " + info.MaDatVanChuyen;
            ExecuteNonQuery(updateCommand);
        }

        public void delete(string maDatVanChuyen)
        {
            string deleteCommand = "DELETE FROM DATVANCHUYEN WHERE MaDatVanChuyen = " + maDatVanChuyen;
            ExecuteNonQuery(deleteCommand);
        }
    }
}
